Para cualquier trabajo intensivo de datos pandas
se ha convertido en la librería esencial dentro del mundo Python.
Se podría ver a
pandas
como numpy arrays con esteroides, esto es, numpy arrays con etiquetas para columnas y filas y soporte mejorado para trabajo con datos heterogéneos.
Pero lo anterior es una definición un tanto superficial. El diablo está en los detalles!!!
.dropna()
, pd.isnull()
).concat
, join
, merge
).groupby
.stack
, unstack
, pivot
, pivot_table
).pandas
.
In [ ]:
# primero hacemos los imports de turno
import os
import datetime as dt
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from IPython.display import display
np.random.seed(19760812)
plt.style.use('bmh')
%matplotlib inline
In [ ]:
print('Python version:')
print(sys.version)
print()
print('Pandas: ', pd.__version__)
print('Numpy: ', np.__version__)
print('Matplotlib: ', mpl.__version__)
In [ ]:
# Leemos los datos del fichero 'model.txt'
ipath = os.path.join('Datos', 'model.txt')
model = pd.read_csv(ipath, sep = "\s*", skiprows = 3,
parse_dates = {'Timestamp': [0, 1]}, index_col = 'Timestamp')
In [ ]:
model.head()
In [ ]:
pd.tools.plotting.scatter_matrix(model.ix[0:1000, 'M(m/s)':'D(deg)'])
In [ ]:
print(model.index[0], model.index[-1], sep = '\n')
In [ ]:
model.mean()
In [ ]:
model.max()
In [ ]:
idx = model.loc[:, 'M(m/s)'].sort_values(ascending = False).index
In [ ]:
pd.tools.plotting.scatter_matrix(model.loc[idx[:1000], 'M(m/s)':'D(deg)'])
In [ ]:
model.loc[:, 'M(m/s)'].plot.hist(bins = np.arange(0, 35))
In [ ]:
model['month'] = model.index.month
model['year'] = model.index.year
In [ ]:
model.groupby(by = ['year', 'month']).mean().plot(y = 'M(m/s)', figsize = (15, 5))
In [ ]:
monthly = model.groupby(by = ['year', 'month']).mean()
monthly['ma'] = monthly.loc[:, 'M(m/s)'].rolling(5, center = True).mean()
monthly.head()
In [ ]:
monthly.loc[:,['M(m/s)', 'ma']].plot(figsize = (15, 6))
In [ ]:
monthly.loc[:, 'M(m/s)'].reset_index().pivot(index = 'year', columns = 'month')
In [ ]:
(monthly.loc[:, 'M(m/s)'].reset_index().pivot(
index = 'year',
columns = 'month'
).T.plot(
figsize = (15, 10),
legend = False
)
)